package com.njust.RateMyTray_backend.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.njust.RateMyTray_backend.entity.Comment;
import com.njust.RateMyTray_backend.vo.CommentVO;
import com.njust.RateMyTray_backend.vo.CommentTrendVO;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.math.BigDecimal;
import java.time.LocalDate;
import java.util.List;

@Mapper
public interface CommentMapper extends BaseMapper<Comment> {
    
    @Select("<script>" +
            "SELECT AVG(rating) FROM t_comment " +
            "<where>" +
            "<if test='startDate != null'> AND commented_at &gt;= #{startDate}</if>" +
            "<if test='endDate != null'> AND commented_at &lt;= #{endDate}</if>" +
            "</where>" +
            "</script>")
    BigDecimal getOverallAverageRating(@Param("startDate") LocalDate startDate, @Param("endDate") LocalDate endDate);

    @Select("<script>" +
            "SELECT COUNT(*) FROM t_comment " +
            "<where>" +
            "rating &gt;= 4" +
            "<if test='startDate != null'> AND commented_at &gt;= #{startDate}</if>" +
            "<if test='endDate != null'> AND commented_at &lt;= #{endDate}</if>" +
            "</where>" +
            "</script>")
    Long countPositiveComments(@Param("startDate") LocalDate startDate, @Param("endDate") LocalDate endDate);

    @Select("SELECT DATE(commented_at) as date, COUNT(*) as commentCount, AVG(rating) as averageRating " +
            "FROM t_comment " +
            "WHERE target_id = #{targetId} AND target_type = #{targetType} AND commented_at >= #{startDate} " +
            "GROUP BY DATE(commented_at) " +
            "ORDER BY date ASC")
    List<CommentTrendVO> getCommentTrend(@Param("targetId") Long targetId, @Param("targetType") String targetType, @Param("startDate") LocalDate startDate);
} 